import tkinter

from sql_handler.handler import *  # sql 接口
from sql_handler.config import *  # sql 配置文件
from tkinter import messagebox  # tkinter 弹窗
from gui import *


class MainUI:
    def __init__(self):
        # 窗口参数
        self.window = tk.Tk()
        self.window.title('宾馆客房管理系统')
        self.window.maxsize(2000, 600)
        self.window.minsize(800, 600)
        self.window.geometry('800x600+330+180')

        # 登录界面参数
        self.usr_name = tk.StringVar()
        self.usr_pwd = tk.StringVar()

        # -------开发参数！！！ 记得删除-----------------------------
        self.usr_name.set(username)
        self.usr_pwd.set(password)
        # -----------------------------------------------------

        # 主界面参数
        self.normal_mode = tk.StringVar()

        self.hd = handler()
        # （页面）框架列表
        self.login_page = None  # 登录页面
        self.normal_page = None  # 主页面
        self.head_info = None  # 头注
        self.foot_info = None  # 脚注
        self.cust_info = None  # 客户信息查询
        self.book = None  # 预定页面
        self.make_in = None  # 办理入住
        self.make_out = None  # 办理退房
        self.room_info = None  # 查询客房信息
        self.finance_info = None  # 财报系统

        # 开始渲染 login 页面
        self.get_login()

    # 获取 login 页面
    def get_login(self):
        self.login_page = tk.Frame(self.window)
        login_page(self.login_page, self.usr_name, self.usr_pwd, self.login_handle)
        self.login_page.pack(fill='both')

    # 获取主页面
    def get_normal(self):
        self.window.geometry('800x600+330+180')
        self.normal_page = tk.Frame(self.window, bg='#e0e5df')
        normal_page(self.normal_page, self.normal_mode, self.hd, self.normal_handle)
        self.normal_page.pack(fill='both')

    # 获取头部信息和脚注
    def get_headinfo(self):
        self.head_info = tk.Frame(self.window)
        head_handle(self.head_info, self.usr_name.get())
        self.head_info.pack(side='top', fill='both')
        self.foot_info = tk.Frame(self.window, bg='#c1cbd7', height=100)
        foot_info(self.foot_info)
        self.foot_info.pack(side='bottom', fill='x', anchor='s', padx=(0, 0), pady=(0, 0))

    # 获取客户信息查询页面
    def get_customer_info(self):
        self.cust_info = tk.Frame(self.window, bg='white')
        cust_page(self.cust_info, self.get_normal, self.hd)
        self.cust_info.pack(fill='both')

    # 获取房间信息查询页面
    def get_room_info(self):
        self.room_info = tk.Frame(self.window, bg='white')
        room_page(self.room_info, self.get_normal, self.hd)
        self.room_info.pack(fill='both')

    # 获取财务报表系统
    def financial_info(self):
        self.finance_info = tk.Frame(self.window, bg='white')
        financial_page(self.finance_info, self.get_normal, self.hd)
        self.finance_info.pack(fill='both')

    # 获取预订页面
    def book_info(self):
        self.book = tk.Frame(self.window, bg='white')
        book_page(self.book, self.get_normal, self.hd)
        self.book.pack(fill='both')

    # 获取入住办理页面
    def in_info(self):
        self.window.geometry('1000x600+200+180')
        self.make_in = tk.Frame(self.window, bg='white')
        in_page(self.make_in, self.get_normal, self.hd)
        self.make_in.pack(fill='both')

    # 获取退房办理页面
    def out_info(self):
        self.make_out = tk.Frame(self.window, bg='white')
        out_page(self.make_out, self.get_normal, self.hd)
        self.make_out.pack(fill='both')

    # 登录页面响应接口
    def login_handle(self, event=None):
        name = self.usr_name.get()
        pwd = self.usr_pwd.get()
        try:
            self.hd.connect_sql(name, pwd)
        except:
            tk.messagebox.showerror(message='登录失败：请检查用户名和密码。')
            return
        print('log: Successfully connect database.')
        self.usr_pwd.set("")
        self.login_page.destroy()
        # 渲染员工操作页面
        self.get_headinfo()
        self.get_normal()

    # 主页面响应接口
    def normal_handle(self):
        self.normal_page.destroy()
        mode = self.normal_mode.get()
        if mode == 'book':
            self.book_info()
        elif mode == 'exit':
            self.head_info.destroy()
            self.foot_info.destroy()
            self.get_login()
        elif mode == 'in':
            self.in_info()
        elif mode == 'out':
            self.out_info()
        elif mode == 'find':
            self.get_customer_info()
        elif mode == 'room':
            self.get_room_info()
        else:  # model == 'financial'
            self.financial_info()


if __name__ == '__main__':
    ui = MainUI()
    ui.window.mainloop()

